package com.initialize.core.security.handler;


import cn.hutool.json.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/1/5
 * Time: 4:49 下午
 *
 * @Description: 未携带token用户, 或者非法token访问无权限资源时的异常
 */
@Slf4j
@Component
public class BaseAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @SneakyThrows
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) {

        log.info("BaseAuthenticationEntryPoint-->>   ");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 1001);
        jsonObject.put("msg", "无访问权限");

        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().print(jsonObject.toString());


    }
}